﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;

namespace NLBS.Data.Business
{
   public class ArticleFacade
    {
       private Article Art;

       public ArticleFacade() { }

       public ArticleFacade(Article art)
       {
           this.Art=art;
           this.UpdateWhenAddArticle();
       }

       public ArticleFacade(int artID)
       {
           this.UpdateWhenDeleteArticle(artID);
       }

       public static readonly string OneArticleKey = "OneArticleKey:ID{0}";
       public static readonly String ArticleArrayKey = "ArticleCatArray{0}";

       public void UpdateWhenDeleteArticle(int artID)
       {
           ContentCache cache = ContentCache.Instantiate();
           cache.Remove(string.Format(OneArticleKey, artID));
           cache.Remove("CategoryKey");
           cache.Remove("ArchiveList");
           cache.Remove("DefautltArticlePageKey");
           
       }

       public void UpdateWhenAddArticle()
       {
           Article item = Art;
           ContentCache cache = ContentCache.Instantiate();
           //update user article count
           UserData.GetUserByID(item.AuthorID).ArticleCount += 1;
           //update category article count
           Category updateCat = CategoryData.GetCategoryByID(item.CategoryID);
           updateCat.ArticleCount += 1;
           item.Category = updateCat;
           //update global article count
           BlogInfo.Instance().ArticleCount += 1;
           //update default articlelist
           IList<int> listKey = (List<int>)cache["DefautltArticlePageKey"];
           if (listKey != null)
               listKey.Insert(0, item.ArticleID);
           //update catarticlelist
           IList<int> catlistKey = (List<int>)cache[string.Format(ArticleArrayKey, item.CategoryID)];
           if (catlistKey != null)
               catlistKey.Insert(0, item.CategoryID);
           //insert cache artilce
           cache.Insert(string.Format(OneArticleKey, item.ArticleID), item, CacheDuration.Long);
           //update top article
           if (item.Mode == 1 && !updateCat.Hidden)
           {
             ArticleData.GetTopArticle().Insert(0, item);
           }
       }
    }
}
